﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ABCLogistic_System_V2._0.DA;

namespace ABCLogistic_System_V2._0.GUI
{
    public partial class frmQTHT_Phanquyen : Form
    {
        ABCLogisticsEntities context = new ABCLogisticsEntities();
        NHANVIEN nhanvien = new NHANVIEN();
        TAIKHOAN taikhoan = new TAIKHOAN();
        public frmQTHT_Phanquyen()
        {
            InitializeComponent();
        }

        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }

        private void radnhom_CheckedChanged(object sender, EventArgs e)
        {
            if (radnhom.Checked)
            {
                chkchamdut.Enabled = false;
            }
            else
            {
                chkchamdut.Enabled = true;
            }
            var nhom = from cat in context.NHOMNGUOIDUNGs
                       select cat;
            cbxtimkiem.DataSource = nhom.ToList<NHOMNGUOIDUNG>();
            cbxtimkiem.DisplayMember = "TENNHOM";
            cbxtimkiem.ValueMember = "MANHOM";

        }

        private void radguoidung_CheckedChanged(object sender, EventArgs e)
        {
            var nhanvien = from cat in context.NHANVIENs
                           select cat;
            cbxtimkiem.DataSource = nhanvien.ToList<NHANVIEN>();
            cbxtimkiem.DisplayMember = "HOVATEN";
            cbxtimkiem.ValueMember = "MANV";


        }

        private void cbxtimkiem_TextChanged(object sender, EventArgs e)
        {
            string strtimnhanvien = cbxtimkiem.Text;
            NHANVIEN obj = context.NHANVIENs.Where(p => p.HOVATEN == strtimnhanvien).FirstOrDefault();
            //var timnhanvien = from p in context.NHANVIENs
            //                  where nhanvien.HOVATEN == strtimnhanvien
            //                  select p;
            //cbxtimkiem.DataSource = timnhanvien.ToList<NHANVIEN>();
        }

        private void btnthemnhom_Click(object sender, EventArgs e)
        {
            frmQTHT_Nhomnguoidung themnhom = new frmQTHT_Nhomnguoidung();
            themnhom.nhomok += new frmQTHT_Nhomnguoidung.LoadNhom(themnhom_nhomok);
            themnhom.ShowDialog();
        }

        void themnhom_nhomok(bool bchecknhom)
        {
            if (bchecknhom == true)
            {
                var tenquyen = from cat in context.NhomQuyens select new { cat.TENQUYEN };
                grdquyen.DataSource = tenquyen.ToList();
            }
        }

        private void frmQTHT_Phanquyen_Load(object sender, EventArgs e)
        {
            if (cbxtimkiem.Text != "")
            {
                var tenquyen = from cat in context.NhomQuyens select new { cat.TENQUYEN };
                grdquyen.DataSource = tenquyen.ToList();
                var nhom = from cat in context.NHOMNGUOIDUNGs
                           select cat;
                cbxtimkiem.DataSource = nhom.ToList<NHOMNGUOIDUNG>();
                cbxtimkiem.DisplayMember = "TENNHOM";
                cbxtimkiem.ValueMember = "MANHOM";
            }
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            if (chkchamdut.Checked)
            {
                string smanv = cbxtimkiem.SelectedValue.ToString();
                taikhoan = context.TAIKHOANs.Single(p => p.TENDANGNHAP == smanv);
                taikhoan.TRANGTHAI = 0;
                context.AcceptAllChanges();
            }
            else
            {
                //cap nhat quyen nguoi dung
                if (radguoidung.Checked)
                {
                    string smanv = "", stenquyen = "";
                    smanv = cbxtimkiem.SelectedValue.ToString();
                    int itao, ixem, ixoa, isua;
                    for (int i = 0; i < 14; i++)
                    {
                        stenquyen = grdquyen.Rows[i].Cells[0].Value.ToString();
                        itao = int.Parse(grdquyen.Rows[i].Cells[1].Value.ToString());
                        ixem = int.Parse(grdquyen.Rows[i].Cells[2].Value.ToString());
                        ixoa = int.Parse(grdquyen.Rows[i].Cells[3].Value.ToString());
                        isua = int.Parse(grdquyen.Rows[i].Cells[4].Value.ToString());

                        context.SP_QUYENV_UPDATE(smanv, stenquyen, itao, ixem, ixoa, isua);

                    }

                    MessageBox.Show("Lưu thành công!");
                }
                //cap nhat quyen nhom
                if (radnhom.Checked)
                {
                    string smanhom = "", stenquyen = "";
                    smanhom = cbxtimkiem.SelectedValue.ToString();
                    int itao, ixem, ixoa, isua;
                    for (int i = 0; i < 14; i++)
                    {
                        stenquyen = grdquyen.Rows[i].Cells[0].Value.ToString();
                        itao = int.Parse(grdquyen.Rows[i].Cells[1].Value.ToString());
                        ixem = int.Parse(grdquyen.Rows[i].Cells[2].Value.ToString());
                        ixoa = int.Parse(grdquyen.Rows[i].Cells[3].Value.ToString());
                        isua = int.Parse(grdquyen.Rows[i].Cells[4].Value.ToString());
                        context.SP_QUYENNHOM_UPDATE(smanhom, stenquyen, itao, ixem, ixoa, isua);
                    }
                    MessageBox.Show("Lưu thành công!");
                }
                if (chkchamdut.Checked)
                {

                }
            }
        }

        private void cbxtimkiem_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ma = cbxtimkiem.SelectedValue.ToString();
            if (cbxtimkiem.SelectedIndex >= 0)
            {
                try
                {
                    if (radguoidung.Checked)
                    {
                        string manv = cbxtimkiem.SelectedValue.ToString();
                        var quyennguoidung = context.SP_LAYQUYENNV_SELECT(manv);
                        grdquyen.DataSource = quyennguoidung.ToList();
                    }
                    if (radnhom.Checked)
                    {
                        string manhom = cbxtimkiem.SelectedValue.ToString();
                        var quyennguoidung = context.SP_LAYQUYENNHOM_SELECT(manhom);
                        grdquyen.DataSource = quyennguoidung.ToList();
                    }
                }
                catch
                {
                }
            }

        }
    }
}
